package com.genexususa.eo.bing.connection;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.GxItemException;
import com.genexususa.eo.bing.data.Info;
import com.genexususa.eo.bing.data.Util;
import com.genexususa.eo.bing.data.results.*;
import com.genexususa.eo.bing.data.wsSubclasses.GxSitePlacement;
import com.genexususa.eo.bing.data.requests.GxSitePlacementsAdGroupRequest;
import com.genexususa.eo.bing.data.requests.GxSitePlacementsRequest;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.io.StringWriter;
import java.rmi.RemoteException;
import java.util.HashMap;

public class SitePlacementManager {
    public SitePlacementManager() {
    }

    public String addSitePlacements(long customerAccountId, String sitePlacementsRequestsXML) {

        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        AddSitePlacementsRequest addSitePlacementsRequest = null;
        AddSitePlacementsResponse addSitePlacementsResponse = null;
        GxElementsResults result = null;
        String trackingId = "";
        Util util = new Util();
        GxSitePlacementsRequest[] sitePlacementsRequests = null;
        GxSitePlacementsAdGroupRequest sitePlacementsAdGroupRequest = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Parse sitePlacementsRequests from XML
            sitePlacementsRequests = util.sitePlacementsRequestsFromXML(sitePlacementsRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (sitePlacementsRequests.length == 0) {
                throw new GxItemException("No SitePlacementsRequests were found");
            }

            result = new GxElementsResults(sitePlacementsRequests.length);
            for (int i=0; i<sitePlacementsRequests.length; i++) {
                result.addResult(i, new GxElementsCampaignsResult(sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length));
                for (int j=0; j<sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length; j++) {
                    result.getElementsCampaignsResultCollection()[i].addResult(j, new GxElementsAdGroupsResult());
                }
            }
            for (int k=0; k<sitePlacementsRequests.length; k++) {
                result.getElementsCampaignsResultCollection()[k].setGxCampaignId(sitePlacementsRequests[k].getGxCampaignId());
                result.getElementsCampaignsResultCollection()[k].setBingCampaignId(sitePlacementsRequests[k].getBingCampaignId());
                for (int j=0; j<sitePlacementsRequests[k].getSitePlacementsAdGroupRequests().length; j++) {
                    sitePlacementsAdGroupRequest = sitePlacementsRequests[k].getSitePlacementsAdGroupRequests()[j];

                    // Set body parameters
                    addSitePlacementsRequest = new AddSitePlacementsRequest();
                    addSitePlacementsRequest.setAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                    addSitePlacementsRequest.setSitePlacements(sitePlacementsAdGroupRequest.getSitePlacements());

                    try {
                        // Call WS
                        addSitePlacementsResponse = campaignManagement.addSitePlacements(addSitePlacementsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                        // Call succeeded with no errors

                        // Create the return object
                        trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                        result.trackingId = trackingId;


                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i<addSitePlacementsResponse.getSitePlacementIds().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId(), addSitePlacementsResponse.getSitePlacementIds()[i]);
                        }
                    } catch (ApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addApiFaultDetailError(fault);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    } catch (AdApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addAdApiFaultDetailError(fault);
                        result.setError();
                    } catch (RemoteException e) { // IN CALL
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addRemoteExceptionError(e);
                        result.setError();
                    }
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIllegalArgumentExceptionError(e);
        } catch (GxItemException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addGxItemExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxElementsResults(e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxElementsCampaignsResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addSAXExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxElementsResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxElementsResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String updateSitePlacements(long customerAccountId, String sitePlacementsRequestsXML) {

        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        UpdateSitePlacementsRequest updateSitePlacementsRequest = null;
        GxElementsResults result = null;
        String trackingId = "";
        Util util = new Util();
        GxSitePlacementsRequest[] sitePlacementsRequests = null;
        GxSitePlacementsAdGroupRequest sitePlacementsAdGroupRequest = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Parse sitePlacementsRequests from XML
            sitePlacementsRequests = util.sitePlacementsRequestsFromXML(sitePlacementsRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (sitePlacementsRequests.length == 0) {
                throw new GxItemException("No SitePlacementsRequests were found");
            }
            result = new GxElementsResults(sitePlacementsRequests.length);
            for (int i=0; i<sitePlacementsRequests.length; i++) {
                result.addResult(i, new GxElementsCampaignsResult(sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length));
                for (int j=0; j<sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length; j++) {
                    result.getElementsCampaignsResultCollection()[i].addResult(j, new GxElementsAdGroupsResult());
                }
            }
            for (int k=0; k<sitePlacementsRequests.length; k++) {
                result.getElementsCampaignsResultCollection()[k].setGxCampaignId(sitePlacementsRequests[k].getGxCampaignId());
                result.getElementsCampaignsResultCollection()[k].setBingCampaignId(sitePlacementsRequests[k].getBingCampaignId());
                for (int j=0; j<sitePlacementsRequests[k].getSitePlacementsAdGroupRequests().length; j++) {
                    sitePlacementsAdGroupRequest = sitePlacementsRequests[k].getSitePlacementsAdGroupRequests()[j];

                    // Set body parameters
                    updateSitePlacementsRequest = new UpdateSitePlacementsRequest();
                    updateSitePlacementsRequest.setAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                    updateSitePlacementsRequest.setSitePlacements(sitePlacementsAdGroupRequest.getSitePlacements());

                    try {
                        // Call WS
                        campaignManagement.updateSitePlacements(updateSitePlacementsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                        // Call succeeded with no errors

                        // Create the return object
                        trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId(), sitePlacementsAdGroupRequest.getSitePlacements()[i].getId());
                        }
                    } catch (ApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addApiFaultDetailError(fault);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    } catch (AdApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addAdApiFaultDetailError(fault);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    } catch (RemoteException e) { // IN CALL
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addRemoteExceptionError(e);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    }
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIllegalArgumentExceptionError(e);
        } catch (GxItemException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addGxItemExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxElementsResults(e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxElementsCampaignsResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addSAXExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxElementsResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxElementsResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String deleteSitePlacements(long customerAccountId, String sitePlacementsRequestsXML) {

        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        DeleteSitePlacementsRequest deleteSitePlacementsRequest = null;
        GxElementsResults result = null;
        String trackingId = "";
        Util util = new Util();
        GxSitePlacementsRequest[] sitePlacementsRequests = null;
        GxSitePlacementsAdGroupRequest sitePlacementsAdGroupRequest = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Parse sitePlacementRequests from XML
            sitePlacementsRequests = util.sitePlacementsRequestsFromXML(sitePlacementsRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (sitePlacementsRequests.length == 0) {
                throw new GxItemException("No SitePlacementsRequests were found");
            }
            result = new GxElementsResults(sitePlacementsRequests.length);
            for (int i=0; i<sitePlacementsRequests.length; i++) {
                result.addResult(i, new GxElementsCampaignsResult(sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length));
                for (int j=0; j<sitePlacementsRequests[i].getSitePlacementsAdGroupRequests().length; j++) {
                    result.getElementsCampaignsResultCollection()[i].addResult(j, new GxElementsAdGroupsResult());
                }
            }
            for (int k=0; k<sitePlacementsRequests.length; k++) {
                result.getElementsCampaignsResultCollection()[k].setGxCampaignId(sitePlacementsRequests[k].getGxCampaignId());
                result.getElementsCampaignsResultCollection()[k].setBingCampaignId(sitePlacementsRequests[k].getBingCampaignId());
                for (int j=0; j<sitePlacementsRequests[k].getSitePlacementsAdGroupRequests().length; j++) {
                    sitePlacementsAdGroupRequest = sitePlacementsRequests[k].getSitePlacementsAdGroupRequests()[j];

                    // Set body parameters
                    deleteSitePlacementsRequest = new DeleteSitePlacementsRequest();
                    deleteSitePlacementsRequest.setAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                    long[] sitePlacementIds = new long[sitePlacementsAdGroupRequest.getSitePlacements().length];
                    for (int i=0; i<sitePlacementIds.length; i++) {
                        if (sitePlacementsAdGroupRequest.getSitePlacements()[i].getId() == null) {
                            sitePlacementIds[i] = -1;
                        } else {
                            sitePlacementIds[i] = sitePlacementsAdGroupRequest.getSitePlacements()[i].getId();
                        }
                    }
                    deleteSitePlacementsRequest.setSitePlacementIds(sitePlacementIds);

                    try {
                        // Call WS
                        campaignManagement.deleteSitePlacements(deleteSitePlacementsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                        // Call succeeded with no errors

                        // Create the return object
                        trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId(), sitePlacementsAdGroupRequest.getSitePlacements()[i].getId());
                        }
                    } catch (ApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addApiFaultDetailError(fault);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    } catch (AdApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setSpecificTrackingId(trackingId);
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addAdApiFaultDetailError(fault);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    } catch (RemoteException e) { // IN CALL
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setGxAdGroupId(sitePlacementsAdGroupRequest.getGxAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].setBingAdGroupId(sitePlacementsAdGroupRequest.getBingAdGroupId());
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].createElementCollection(sitePlacementsAdGroupRequest.getSitePlacements().length);
                        for (int i=0; i< sitePlacementsAdGroupRequest.getSitePlacements().length; i++) {
                            result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addElementResult(i, ((GxSitePlacement) sitePlacementsAdGroupRequest.getSitePlacements()[i]).getGxId());
                        }
                        result.getElementsCampaignsResultCollection()[k].getElementsAdGroupsResults()[j].addRemoteExceptionError(e);
                        result.setError();
                        result.getElementsCampaignsResultCollection()[k].setError();
                    }
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIllegalArgumentExceptionError(e);
        } catch (GxItemException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addGxItemExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxElementsResults(e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxElementsCampaignsResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addSAXExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxElementsResults();
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxElementsResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxElementsResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }
}
